package com.heima.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.heima.common.PageResult;
import com.heima.common.Result;
import com.heima.common.ResultQuery;
import com.heima.pojo.dto.AddRegionDto;
import com.heima.pojo.entity.Region;
import com.heima.service.RegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 区域管理
 */
@RestController
@RequestMapping("/api/vm-service/region")
@Slf4j
@Api("区域管理")
public class RegionController {


    @Autowired
    private RegionService regionService;

    /**
     * 区域搜索
     *
     * @param query
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("区域列表")
    public PageResult<Region> search(ResultQuery query) {
        return regionService.searchQuly(query);
    }

    /**
     * 新增区域
     *
     * @param dto 区域信息
     * @return 操作结果
     */
    @PostMapping
    @ApiOperation("新增区域")
    public Result<Boolean> addRegion(@RequestBody AddRegionDto dto) {
        log.info("新增区域: {},", dto);
        Result<Boolean> add = regionService.add(dto);
        return add;

    }


    /**
     * 删除区域
     * @param regionId
     * @return
     */
    @DeleteMapping("/{regionId}")
    @ApiOperation("删除区域")
    public Result deleteRegion(@PathVariable String regionId) {
        log.info("删除区域: {}", regionId);
        try {
            Long id = Long.parseLong(regionId);
            regionService.removeById(id);
            return Result.success();
        } catch (NumberFormatException e) {
            return Result.error("区域ID格式不正确");
        }
    }

    




}
